﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using Repository;

namespace Dispatcher.DataTemplateSelectors
{
    /// <summary>
    /// 
    /// </summary>
    internal class DataGridDataTemplateSelector : DataTemplateSelector
    {
        /// <summary>
        /// When overridden in a derived class, returns a <see cref="T:System.Windows.DataTemplate"/> based on custom logic.
        /// </summary>
        /// <param name="item">The data object for which to select the template.</param>
        /// <param name="container">The data-bound object.</param>
        /// <returns>
        /// Returns a <see cref="T:System.Windows.DataTemplate"/> or null. The default value is null.
        /// </returns>
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            DataTemplate template = null;
            if (item != null)
            {
                var element = container.GetNearestParent<Window>();
                if (element != null)
                {
                    var templateName = "Default";

                    if (item is ObservableCollection<Person>)
                    {
                        templateName = "Person";
                    }

                    if (item is ObservableCollection<PersonGroup>)
                    {
                        templateName = "PersonGroup";
                    }

                    template = element.FindResource(templateName) as DataTemplate;
                }
            }

            return template;
        }
    }
}
